<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="选择客户"
    :height="400"
    :width="1200"
    :padding="5"
  >
    <div>
      <client ref="client"></client>
    </div>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="onSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
  <vol-box
    :lazy="true"
    v-model="detailModel"
    title="选择商品"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <div>
      <goodsInfo ref="goodsInfo"></goodsInfo>
    </div>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="detailOnSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
  <vol-box
    :lazy="true"
    v-model="detailWarehouseModel"
    title="选择仓库"
    :height="400"
    :width="1200"
    :padding="5"
  >
    <div>
      <warehouse ref="warehouse"></warehouse>
    </div>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="detailWarehouseOnSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
<script>
import VolBox from "@/components/basic/VolBox.vue";
import client from "@/views/customerinfo/q_client/q_client.vue";
import goodsInfo from "@/views/basicinfo/goods_info/q_goods_info.vue";
import warehouse from "@/views/inventoryinfo/warehouse/q_warehouse.vue";
//这里使用的vue2语法，也可以写成vue3语法
export default {
  components: {
    "vol-box": VolBox,
    client,
    goodsInfo,
    warehouse,
  },
  data() {
    return {
      model: false,
      detailModel: false,
      detailWarehouseModel: false,
      // purchaseApplicationModel: false,
    };
  },
  methods: {
    async onModelClose() {
      this.type = null;
      this.goodsFittingsDetail = null;
      this.$refs.goodsInfo.searchFormFields.Item_sub = null;
      this.$refs.goodsInfo.searchFormFields.Item_name = null;
      this.$refs.goodsInfo.searchFormFields.Item_clsno = null;
      this.$refs.goodsInfo.searchFormFields.Item_brandname = null;
      this.$refs.goodsInfo.searchFormFields.Display_flag = null;
      this.$refs.goodsInfo.searchFormFields.Supplier = null;
      // this.$refs.goodsInfo.searchFormFields.AuditStatus = "1";
      this.$refs.goodsInfo.searchFormFields.Item_subno = null;
      this.$refs.goodsInfo.searchFormFields.Cost_accounting= null;
      await this.$refs.goodsInfo.$refs.grid.search();
    },
    open() {
      this.model = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.goodsInfo) {
          clearInterval(setIntervalTiem);
          // this.$refs.goodsInfo.$refs.grid.search();
        }
      }, 100);
    },
    detailOpen(value) {
      this.type = value;
      this.detailModel = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.goodsInfo) {
          clearInterval(setIntervalTiem);
          // this.$refs.goodsInfo.searchFormFields.AuditStatus = "1";
          this.$refs.goodsInfo.searchFormFields.Cost_accounting = null;
          this.$refs.goodsInfo.searchFormFields.Item_sub = null;
          this.$refs.goodsInfo.searchFormFields.Item_name = null;
          this.$refs.goodsInfo.searchFormFields.Item_clsno = null;
          this.$refs.goodsInfo.searchFormFields.Item_brandname = null;
          this.$refs.goodsInfo.searchFormFields.Item_subno = null;
          this.$refs.goodsInfo.searchFormFields.Display_flag = null;
          this.$refs.goodsInfo.searchFormFields.Supplier = null;
          // this.$refs.goodsInfo.$refs.grid.search()
        }
      }, 100);
    },
    goodsFittingsOpen(value) {
      this.type = value;
      this.detailModel = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.goodsInfo) {
          clearInterval(setIntervalTiem);
          this.$refs.goodsInfo.searchFormFields.Item_brandname = "1";
          this.$refs.goodsInfo.$refs.grid.search();
        }
      }, 100);
    },
    goodsFittingsDetailOpen(value, Detail) {
      this.type = value;
      this.goodsFittingsDetail = Detail;
      this.detailModel = true;
      // let setIntervalTiem = setInterval(() => {
      //   if (this.$refs.goodsInfo) {
      //     clearInterval(setIntervalTiem);
      //     this.$refs.goodsInfo.searchFormFields.Item_brandname =null;
      //     this.$refs.goodsInfo.searchFormFields.Item_subno = null;
      //     this.$refs.goodsInfo.searchFormFields.Display_flag = null;
      //     this.$refs.goodsInfo.searchFormFields.Supplier = null;
      //     this.$refs.goodsInfo.searchFormFields.AuditStatus = "1";
      //     this.$refs.goodsInfo.searchFormFields.Item_sub = null;
      //     this.$refs.goodsInfo.searchFormFields.Item_name = null;
      //     this.$refs.goodsInfo.searchFormFields.Item_clsno = null;
      //     this.$refs.goodsInfo.$refs.grid.search();
      //   }
      // }, 100);
    },
    detailWarehouseOpen() {
      this.detailWarehouseModel = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.goodsInfo) {
          clearInterval(setIntervalTiem);
          this.$refs.warehouse.$refs.grid.search();
        }
      }, 100);
    },

    onSelect() {
      let rowObj = this.$refs.client.$refs.grid.getSelectRows();
      if (!rowObj.length) {
        return this.$message.error("请选择客户");
      }
      this.$emit("parentCall", ($parent) => {
        //这里还要设置客户ID,避免名称改动查找不到目标
        if (this.$route.path == "/q_goodsIn_class") {
          $parent.editFormFields.GoodsIn_client = rowObj[0].Client_name;
        } else if (this.$route.path == "/q_goodsOut_class") {
          $parent.editFormFields.GoodsOut_client = rowObj[0].Client_name;
        }
      });
      return (this.model = false);
    },
    async detailOnSelect() {
      let rowObj = this.$refs.goodsInfo.$refs.grid.getSelectRows();
      /*
      if (this.$route.path == "/q_serve_contract") {
        this.$emit("parentCall", ($parent) => {
          if (rowObj.length) {
            this.$emit("parentCall", ($parent) => {
              let detailRowData = $parent.$refs.detail.rowData;
              let num = 0;
              detailRowData.forEach((item) => {
                rowObj.forEach((o) => {
                  if (o.Item_sub == item.Goods_sub) num++;
                });
              });

              if (!num) {
                let newRowObj = rowObj.map((item) => {
                  return {
                    Goods_sub: item.Item_sub,
                    Goods_name: item.Item_name,
                    Goods_num: 1,
                    Goods_size: item.Item_size,
                    Total_price: item.Sale_price,
                    Goods_price: item.Sale_price,
                  };
                });
                this.$emit("parentCall", ($parent) => {
                  $parent.$refs.detail.rowData.unshift(...newRowObj);
                });
                this.onModelClose();
                return (this.detailModel = false);
              } else {
                this.$message.error("此采服务合同已包含该商品");
              }
            });
            this.onModelClose();
            return (this.detailModel = false);
          } else {
            return this.$message.error("请选择商品");
          }
        });
      }
*/
      if (this.$route.path == "/q_goods_fittings") {
        if (rowObj.length) {
          if (this.goodsFittingsDetail == "明细表") {
            this.$emit("parentCall", ($parent) => {
              let num = 0;
              $parent.$refs.detail.rowData.forEach((item) => {
                //判断新增商品和原有的商品的进行对比
                rowObj.forEach((o) => {
                  if (o.Item_sub == item.Goods_sub) num++;
                });
              });
              if (!num) {
                let newRowObj = rowObj.map((item) => {
                  return {
                    Goods_sub: item.Item_sub,
                    Goods_name: item.Item_name,
                    Goods_size: item.Item_size,
                    Item_clsno: item.Item_clsno,
                    Item_brand: item.Item_brand,
                    Item_subno: item.Item_subno,
                    Sale_price: item.Sale_price,
                  };
                });
                $parent.$refs.detail.rowData.unshift(...newRowObj);
                this.onModelClose();
                return (this.detailModel = false);
              } else {
                this.base.ElMessageFn("此明细单已包含该商品");
              }
            });
          } else {
            if (rowObj.length > 1) {
              this.base.ElMessageFn("请选择一个商品");
              return;
            }
            try {
              let Item_brandnameSome = rowObj.some(
                (item) => item.Item_brandname != 1
              );
              if (Item_brandnameSome) {
                this.base.ElMessageFn("请选择自研商品");
                return;
              }
              this.$emit("parentCall", ($parent) => {
                $parent.editFormFields.Goods_sub = rowObj[0].Item_sub;
                $parent.editFormFields.Goods_name = rowObj[0].Item_name;
              });
              this.onModelClose();
              return (this.detailModel = false);
            } catch (error) {
              console.error(error);
            }
          }
        } else {
          this.$message.error("请至少选择一个商品");
        }
      }
      if (this.$route.path == "/q_goods_inventory") {
        this.$emit("parentCall", ($parent) => {
          if (rowObj.length) {
            this.$emit("parentCall", ($parent) => {
              $parent.searchFormFields.goods_code = rowObj[0].Item_sub;
            });
            this.onModelClose();
            return (this.detailModel = false);
          } else {
            return this.$message.error("请选择要查询的商品");
          }
        });
      }
      if (
        // this.$route.path == "/q_other_in_out" ||
        this.$route.path == "/q_overflow_voucher"
      ) {
        this.$emit("parentCall", ($parent) => {
          if (rowObj.length) {
            this.$emit("parentCall", ($parent) => {
              let detailRowData = $parent.$refs.detail.rowData;
              let otherNum = 0;
              detailRowData.forEach((item) => {
                rowObj.forEach((o) => {
                  // if (o.Item_sub == item.Goods_sub&&o.WHSE_name == item.WHSE_name) otherNum++;
                });
              });
              if (!otherNum) {
                let newRowObj = rowObj.map((item) => {
                  let obj = {
                    Goods_sub: item.Item_sub,
                    Goods_name: item.Item_name,
                    Goods_size: item.Item_size,
                    Cost_accounting: item.Cost_accounting,
                    Tax_rate: item.Tax_rate,
                    Type: this.type,
                  };
                  if (this.type == "赠品") {
                    obj.Goods_price = 0;
                  }
                  return obj;
                });
                this.$emit("parentCall", ($parent) => {
                  $parent.$refs.detail.rowData.unshift(...newRowObj);
                });
                // this.onModelClose();
                // return (this.detailModel = false);
              } else {
                this.$message.error("此申请单已包含该商品");
              }
            });
            this.onModelClose();
            return (this.detailModel = false);
          } else {
            return this.$message.error("请选择商品");
          }
        });
      }
      if (this.$route.path == "/q_purchase_application") {
        this.$emit("parentCall", ($parent) => {
          if (rowObj.length) {
            this.$emit("parentCall", ($parent) => {
              let detailRowData = $parent.$refs.detail.rowData;
              let num = 0;
              detailRowData.forEach((item) => {
                rowObj.forEach((o) => {
                  if (o.Item_sub == item.Goods_sub) num++;
                });
              });

              if (!num) {
                let newRowObj = rowObj.map((item) => {
                  return {
                    Goods_sub: item.Item_sub,
                    Goods_name: item.Item_name,
                    Goods_size: item.Item_size,
                    Tax_rate: item.Tax_rate,
                    Purchase_state: 0,
                  };
                });
                this.$emit("parentCall", ($parent) => {
                  $parent.$refs.detail.rowData.unshift(...newRowObj);
                });
                this.onModelClose();
                return (this.detailModel = false);
              } else {
                this.$message.error("此采购申请单已包含该商品");
              }
            });
            this.onModelClose();
            return (this.detailModel = false);
          } else {
            return this.$message.error("请选择商品");
          }
        });
      }
      if (this.$route.path == "/q_warehouse_location") {
        if (!rowObj.length) {
          return this.$message.error("请选择商品");
        }
        this.$emit("parentCall", ($parent) => {
          let detailRowData = $parent.$refs.detail.rowData;
          let num = 0;
          detailRowData.forEach((item) => {
            //判断新增商品和原有的商品的进行对比
            rowObj.forEach((o) => {
              if (o.Item_name == item.Item_name) num++;
            });
          });
          if (!num) {
            let newRowObj = rowObj.map((item) => {
              return {
                Item_sub: item.Item_sub,
                Item_name: item.Item_name,
              };
            });
            this.$emit("parentCall", ($parent) => {
              $parent.$refs.detail.rowData.unshift(...newRowObj);
            });

            this.onModelClose();
            return (this.detailModel = false);
          } else {
            this.$message.error("库位已存在此商品");
          }
        });
      } else if (this.$route.path == "/q_goodsOut_class") {
        // alert("inventoryinfo/goodsin_class/goodsInCom.vue监测库位请求");
        /*
        if (!rowObj.length) {
          return this.$message.error("请选择商品");
        }
        this.$emit("parentCall", ($parent) => {
          let detailRowData = $parent.$refs.detail.rowData;
          let num = 0;
          detailRowData.forEach((item) => {
            rowObj.forEach((o) => {
              if (o.Item_sub == item.GoodsOutList_sub) num++;
            });
          });

          if (!num) {
            let newRowObj = rowObj.map((item) => {
              return {
                GoodsOutList_sub: item.Item_sub,
                GoodsOutList_name: item.Item_name,
                GoodsOutList_size: item.Item_size,
                GoodsOutList_price: item.Sale_price,
                //   GoodsOutList_warehouse:item.Warehouse,
              };
            });
            this.$emit("parentCall", ($parent) => {
              $parent.$refs.detail.rowData.unshift(...newRowObj);
              this.getDatileSelectFn($parent, "GoodsOutList_sub");
            });
            this.onModelClose();
            return (this.detailModel = false);
          } else {
            this.$message.error("此出库单已包含该商品");
          }
        });
       */
      }

      //#region
      // else if (
      //   this.$route.path == "/q_warehouse_allot"
      //   // ||  this.$route.path == "/q_stock_out"
      // ) {
      //   alert(1);
      //   if (!rowObj.length) {
      //     return this.$message.error("请选择商品");
      //   }
      //   this.$emit("parentCall", ($parent) => {
      //     let detailRowData = $parent.$refs.detail.rowData;
      //     let num = 0;
      //     detailRowData.forEach((item) => {
      //       rowObj.forEach((o) => {
      //         if (o.Item_sub == item.Goods_sub) num++;
      //       });
      //     });
      //     if (!num) {
      //       let newRowObj = rowObj.map((item) => {
      //         return {
      //           Goods_sub: item.Item_sub,
      //           Goods_name: item.Item_name,
      //           Goods_size: item.Item_size,
      //           Goods_price: item.Sale_price,
      //         };
      //       });
      //       this.$emit("parentCall", ($parent) => {
      //         $parent.$refs.detail.rowData.unshift(...newRowObj);
      //         // if (this.$route.path == "/q_stock_out") {
      //         //   alert(1);
      //         //   this.getDatileSelectFn($parent, "Goods_sub");
      //         // }
      //       });
      //       this.onModelClose();
      //       return (this.detailModel = false);
      //     } else {
      //       this.$message.error("此调拨单已包含该商品");
      //     }
      //   });
      // }
      //#endregion

      if (
        this.$route.path == "/q_goodsIn_class" ||
        this.$route.path == "/q_stock_in" ||
        // this.$route.path == "/q_produce_return" ||
        // this.$route.path == "/q_produce_use" ||
        // this.$route.path == "/q_produce_notarize" ||
        this.$route.path == "/q_sales_contract"
        //|| this.$route.path == "/q_delivery_request"
      ) {
        if (!rowObj.length) {
          return this.$message.error("请选择商品");
        }
        // 商品信息页面不再展示审核状态
        // let rowObjAuditSome = rowObj.some((item) => item.AuditStatus != 1);
        // if (rowObjAuditSome) {
        //   if (this.messageError) {
        //     this.messageError.close();
        //   }
        //   return (this.messageError =
        //     this.$message.error("请选择审核通过的商品"));
        // }
        //----------------
        this.$emit("parentCall", ($parent) => {
          let detailRowData = $parent.$refs.detail.rowData;
          let num = 0;
          detailRowData.forEach((item) => {
            rowObj.forEach((o) => {
              if (
                o.Item_sub == item.GoodsInList_sub ||
                o.Item_sub == item.Goods_sub
              )
                num++;
            });
          });
          if (!num) {
            let newRowObj = rowObj.map((item) => {
              if (this.$route.path == "/q_goodsIn_class") {
                // alert("inventoryinfo/goodsin_class/goodsInCom.vue监测库位请求");
                return {
                  GoodsInList_sub: item.Item_sub,
                  GoodsInList_name: item.Item_name,
                  GoodsInList_size: item.Item_size,
                  GoodsInList_price: item.Sale_price,
                };
              }
              //#region
              // if (
              //   // this.$route.path == "/q_produce_return" ||
              //   // this.$route.path == "/q_produce_use" ||
              //   // this.$route.path == "/q_produce_notarize" ||
              //   // this.$route.path == "/q_sales_contract" ||
              //   this.$route.path == "/q_delivery_request"
              // ) {
              //   return {
              //     Goods_sub: item.Item_sub,
              //     Goods_name: item.Item_name,
              //     Goods_size: item.Item_size,
              //     Goods_price: item.Sale_price,
              //     Production_state: 1,
              //   };
              // }
              //#endregion
              if (this.$route.path == "/q_stock_in") {
                return {
                  Goods_sub: item.Item_sub,
                  Goods_name: item.Item_name,
                  Goods_size: item.Item_size,
                  Cost_price: item.Price_in,
                  Tax_rate: item.Tax_rate,
                  Tax_exclusive: this.base.roundToFourDecimals(
                    item.Price_in / (1 + +item.Tax_rate)
                  ),
                  Production_state: 1,
                };
              }

              if (this.$route.path == "/q_sales_contract") {
                return {
                  Goods_sub: item.Item_sub,
                  Goods_name: item.Item_name,
                  Goods_num: 1,
                  Goods_size: item.Item_size,
                  Total_price: item.Sale_price,
                  Goods_price: item.Sale_price,
                  Logistics_status: 0,
                  Install_status: 0,
                };
              }
            });
            this.$emit("parentCall", async ($parent) => {
              if (this.$route.path == "/q_goodsIn_class") {
                // alert("inventoryinfo/goodsin_class/goodsInCom.vue监测库位请求");
                this.getDatileSelectFn($parent, "GoodsInList_sub");
                // newRowObj = this.base.getPushDatileWHSEName(newRowObj,"GoodsInList_sub"); 检查上面方法是否替换
              }
              if (
                this.$route.path == "/q_stock_in"
                //|| this.$route.path == "/q_produce_return" ||
                // this.$route.path == "/q_produce_use" ||
                // this.$route.path == "/q_produce_notarize" ||
                // this.$route.path == "/q_sales_contract" || //没有库位字段
                // this.$route.path == "/q_delivery_request"
              ) {
                // this.getDatileSelectFn($parent, "Goods_sub");
                newRowObj = this.base.getPushDatileWHSEName(
                  newRowObj,
                  "Goods_sub"
                );
              }
              $parent.$refs.detail.rowData.unshift(...newRowObj);
            });
            this.onModelClose();
            return (this.detailModel = false);
          } else {
            this.$message.error("此入库单已包含该商品");
          }
        });
        //----------------
      }
    },
    detailWarehouseOnSelect() {
      let rowObj = this.$refs.warehouse.$refs.grid.getSelectRows();
      if (!rowObj.length) {
        return this.$message.error("请选择数据");
      }
      this.$emit("parentCall", ($parent) => {
        let tableData = $parent.$refs.table.rowData;
        if (
          tableData.findIndex((item) => item.WHSE_no == rowObj[0].WHSE_no) != -1
        ) {
          return this.$message.error("列表中已存在当前库位");
        }
        $parent.editFormFields.WHSE_name = rowObj[0].WHSE_name;
        $parent.editFormFields.WHSE_no = rowObj[0].WHSE_no;
        return (this.detailWarehouseModel = false);
      });
    },

    //明细表库位下拉框
    async getDatileSelectFn($parent, sub) {
      try {
        console.log(99);

        let rows = $parent.$refs.detail.rowData;
        await rows.forEach((row) => {
          $parent.$refs.detail.columns.forEach((item) => {
            if (item.field == "WHSE_name") {
              let selectUrl = `api/q_warehouse_locationList/getgoodslocation?bm=${row[sub]}`;
              item.bind.data.forEach((d) => {
                d.hidden = true;
              });
              this.http.post(selectUrl, {}, "").then((reslut) => {
                item.bind.data.forEach((d) => {
                  d.hidden = true;
                });
                item.bind.data.forEach((d) => {
                  reslut.forEach((j) => {
                    if (d.key == j.WHSE_no) {
                      d.hidden = false;
                    }
                  });
                });
                row.WHSE_name = item.bind.data.filter(
                  (x) => x.hidden == false
                )[0].key;
              });
            }
          });
        });
      } catch (error) {
        console.error(error);
      }
    },
  },
};
</script>